/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ServerGUI;


import java.awt.*;
import java.util.LinkedList;
import Const.*;




/**
 *
 * @author cypek
 */
public class ServerGUICanvas extends Canvas implements Runnable {

    private ServerGUIGFX gfx = new ServerGUIGFX();
    private ServerGUIController guiController;
    private Thread t;

    private final float scale = 0.7f;

    private LinkedList<Integer> clientCount;


    public ServerGUICanvas(ServerGUIController gui) {
        this.guiController = gui;

        t = new Thread(this);
        t.start();

    }

    public void drawLayout(Graphics g)
    {
        for(int i = 9; i >= 0; i--){
            //obramowka numerow pieter
            g.drawRect(
                    (int)(scale*40),
                    (int)(scale*(40+80*i)),
                    (int)(40*scale),
                    (int)(40*scale));
            // numerki z pieter
            g.drawImage(gfx.getImageList().get(9-i),
                    (int)(scale*(40)),
                    (int)(scale*(40+80*i)),
                    (int)gfx.resizeWidth(gfx.getImageList().get(9-i), scale),
                    (int)gfx.resizeHeight(gfx.getImageList().get(9-i), scale),
                    this);
            // podloga przed winda
            g.drawImage(ServerGUIGFX.floor,
                    (int)(scale*(350)),
                    (int)(scale*(160+80*(i-1))),
                    (int)(600*scale),
                    (int)(scale*(5)),


                    this);

        }
        // obramowka pod winde ta duza
        g.drawRect((int)(scale*100),
                (int)(scale*0) ,
                (int)(250*scale),
                (int)(scale*800));
    }



    public void drawLift(Graphics g){
        // rysuj winde
        g.drawImage(ServerGUIGFX.cabin,
                (int)(scale*100),
                (int)(scale*(10+(9-guiController.getCurrentFloor())*80)),
                (int)gfx.resizeWidth(ServerGUIGFX.cabin, scale),
                (int)gfx.resizeHeight(ServerGUIGFX.cabin, scale),



                this);
    }

    public void drawLiftDirecion(Graphics g)
    {
            if(guiController.getCurrentDirection() == ServerConst.LiftDirectionDown){
                g.drawImage(ServerGUIGFX.arrowDown,
                    (int)(scale*(0)),
                    (int)(scale*(40+80*(9-guiController.getCurrentFloor()))),
                    (int)gfx.resizeWidth(ServerGUIGFX.arrowDown, scale),
                    (int)gfx.resizeHeight(ServerGUIGFX.arrowDown, scale),
                    this);
            }
            if(guiController.getCurrentDirection() == ServerConst.LiftDirectionUp){
                g.drawImage(ServerGUIGFX.arrowUp,
                    (int)(scale*(0)),
                    (int)(scale*(40+80*(9-guiController.getCurrentFloor()))),
                    (int)gfx.resizeWidth(ServerGUIGFX.arrowDown, scale),
                    (int)gfx.resizeHeight(ServerGUIGFX.arrowDown, scale),
                    this);
            }
    }

    public void drawClientsInsideLift(Graphics g)
    {
       for(int i = 0; i < guiController.getClientLists().getTravelingQueue().getClientList().size(); i++ )
        {
            // rysuj ikonki chopakow w windzie
            g.drawImage(ServerGUIGFX.iconB,
                    (int)(scale*(120 + 45*i)),
                    (int)(scale*(30+(9-guiController.getCurrentFloor())*80)),
                    (int)gfx.resizeWidth(ServerGUIGFX.iconB, scale),
                    (int)gfx.resizeHeight(ServerGUIGFX.iconB, scale),

                    this);

            //bez zmian
            g.drawImage(gfx.getImageListA().get(guiController.getClientLists().getTravelingQueue().getClientList().get(i).getClient().getDesiredFloor()),
                    (int)(scale*(120 + 45*i)),
                    (int)(scale*(10+(9-guiController.getCurrentFloor())*80)),
                     (int)(scale*(20)),
                     (int)(scale*(20)),
                   this);

        }
    }
    public void drawClientWaitingForLift(Graphics g)
    {
        clientCount = new LinkedList<Integer>();
        for(int i = 0; i < ServerConst.LastFloor; i++) { clientCount.add(0); }
        for(int i = 0; i < guiController.getClientLists().getWaitingQueue().getClientList().size(); i++)
        {

            g.drawImage(ServerGUIGFX.icon,
                        (int)(scale*(350 + clientCount.get(guiController.getClientLists().getWaitingQueue().getClientList().get(i).getClient().getCurrentFloor())*40)),
                        (int)(scale*(110+80*(8-guiController.getClientLists().getWaitingQueue().getClientList().get(i).getClient().getCurrentFloor()))),
                        (int)gfx.resizeWidth(ServerGUIGFX.icon, scale),
                        (int)gfx.resizeHeight(ServerGUIGFX.icon, scale),

                        this);



                    if(guiController.getClientLists().getWaitingQueue().getClientList().get(i).getClient().getDesiredDirection() == ServerConst.LiftDirectionUp)
                       {
                           g.drawImage(ServerGUIGFX.arrowUp,
                            (int)(scale*(350 + clientCount.get(guiController.getClientLists().getWaitingQueue().getClientList().get(i).getClient().getCurrentFloor())*40)),
                            (int)(scale*(80+80*(8-guiController.getClientLists().getWaitingQueue().getClientList().get(i).getClient().getCurrentFloor()))),
                            (int)gfx.resizeWidth(ServerGUIGFX.arrowUp, scale),
                            (int)gfx.resizeHeight(ServerGUIGFX.arrowUp, scale),

                            this);
                       }


                    if (guiController.getClientLists().getWaitingQueue().getClientList().get(i).getClient().getDesiredDirection() == ServerConst.LiftDirectionDown)
                       {
                           g.drawImage(ServerGUIGFX.arrowDown,
                            (int)(scale*(350 + clientCount.get(guiController.getClientLists().getWaitingQueue().getClientList().get(i).getClient().getCurrentFloor())*40)),
                            (int)(scale*(80+80*(8-guiController.getClientLists().getWaitingQueue().getClientList().get(i).getClient().getCurrentFloor()))),
                            (int)gfx.resizeWidth(ServerGUIGFX.arrowDown, scale),
                            (int)gfx.resizeHeight(ServerGUIGFX.arrowDown, scale),

                            this);
                       }
            incrementValue(clientCount, guiController.getClientLists().getWaitingQueue().getClientList().get(i).getClient().getCurrentFloor());

        }
    }

    public void drawClientsUndecided(Graphics g)
    {
        clientCount = new LinkedList<Integer>();
        for(int i = 0; i < ServerConst.LastFloor; i++) { clientCount.add(0); }
        for(int i = 0; i < guiController.getClientLists().getWaitingQueue().getClientList().size(); i++)
        {
            // sprwadzam czy moje pietro to przez przypadek nie -1  z clientUndefined
            if(guiController.getClientLists().getWaitingQueue().getClientList().get(i).getClient().getCurrentFloor() >= 0)
            {
                incrementValue(clientCount, guiController.getClientLists().getWaitingQueue().getClientList().get(i).getClient().getCurrentFloor());
            }
        }
        for(int i = 0; i < guiController.getClientLists().getConnectedQueue().getClientList().size(); i++)
        {
            // sprwadzam czy moje pietro to przez przypadek nie -1 z clientUndefined
            if(guiController.getClientLists().getConnectedQueue().getClientList().get(i).getClient().getCurrentFloor() >= 0)
            {
            g.drawImage(ServerGUIGFX.icon,
                        (int)(scale*(350 + clientCount.get(guiController.getClientLists().getConnectedQueue().getClientList().get(i).getClient().getCurrentFloor())*40)),
                        (int)(scale*(110+80*(8-guiController.getClientLists().getConnectedQueue().getClientList().get(i).getClient().getCurrentFloor()))),
                        (int)gfx.resizeWidth(ServerGUIGFX.icon, scale),
                        (int)gfx.resizeWidth(ServerGUIGFX.icon, scale),

                        this);
            g.drawImage(ServerGUIGFX.wat,
                        (int)(scale*(350 + clientCount.get(guiController.getClientLists().getConnectedQueue().getClientList().get(i).getClient().getCurrentFloor())*40)),
                        (int)(scale*(80+80*(8-guiController.getClientLists().getConnectedQueue().getClientList().get(i).getClient().getCurrentFloor()))),
                        (int)gfx.resizeWidth(ServerGUIGFX.wat, scale),
                        (int)gfx.resizeWidth(ServerGUIGFX.wat, scale),

                        this);


            incrementValue(clientCount, guiController.getClientLists().getConnectedQueue().getClientList().get(i).getClient().getCurrentFloor());
            }
        }

    }
    @Override
    public void paint(Graphics g) {

        try{
            drawLayout(g);
            drawLift(g);
            drawLiftDirecion(g);
            if(guiController.getClientLists().getTravelingQueue() != null){
                drawClientsInsideLift(g);
            }
            if(guiController.getClientLists().getWaitingQueue() != null){
                drawClientWaitingForLift(g);
            }
            if(guiController.getClientLists().getConnectedQueue() != null){
                drawClientsUndecided(g);
            }
        }
        catch(Exception ex)
        {
            //System.out.println("ASD");
        }

    }

    public void incrementValue(LinkedList<Integer> list, int i)
    {
        list.set(i , list.get(i)+1);
    }

    @Override
    public void run()
    {
        while(true){
            try{
                this.repaint();
                Thread.sleep(1000);
            }
            catch(Exception ex )
            {
                System.out.println("Sie wywaliem na metodzie run w serverGuiCanvas :x");
            }
        }

    }
}